/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web;

import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.cms.service.CategoryService;

/**
 * 内容管理Controller
 * @author ThinkGem
 * @version 2013-4-21
 */
@Controller
@RequestMapping(value = "${adminPath}/cms")
public class CmsController extends BaseController {

	@Autowired
	private CategoryService categoryService;

    @Autowired
    private SystemService systemService;

    @Autowired
    private OfficeService officeService;

	@RequiresPermissions("cms:view")
	@RequestMapping(value = "")
	public String index() {
		return "modules/cms/cmsIndex";
	}

	@RequiresPermissions("cms:view")
	@RequestMapping(value = "counseling")
	public String counseling() {
		return "modules/cms/counselingIndex";
	}

//    @RequiresPermissions("sys:office:view")
//    @RequestMapping(value = "business")
//    public String cmsBusiness() {
//        return "modules/cms/cmsBusinessList";
//    }

	@RequiresPermissions("cms:view")
	@RequestMapping(value = "classified")
	public String classifiedInfo() {
		return "modules/cms/classifiedInfoIndex";
	}



	@RequiresPermissions("cms:view")
	@RequestMapping(value = "counselingTree")
	public String Counseling(Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//        Office office = new Office();
        User user = systemService.getUser(principal.getId());
        String officeId = "";
//        while (!user.getOffice().getType().equals("5")){
//            officeId = officeService.get(user.getOffice().getParentId()).getId();
//        }
        if(user.getOffice().getType().equals("6")){
            officeId = officeService.get(user.getOffice().getParentId()).getId();
        }else if (user.getOffice().getType().equals("5")){
            officeId = user.getOffice().getId();
        }else{
            officeId = "";
        }
//		 model.addAttribute("categoryList", categoryService.findByUser(true, null));
		model.addAttribute("categoryList", categoryService.findByParentId("12","1"));
        model.addAttribute("officeId",officeId);
		return "modules/cms/counselingTree";
	}
//

    @RequiresPermissions("cms:view")
    @RequestMapping(value = "Business")
    public String cmsBusiness() {
        return "modules/cms/cmsBusinessIndex";
    }

    @RequiresPermissions("cms:view")
    @RequestMapping(value = "cmsBusinessTree")
    public String cmsBusiness(Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//        Office office = new Office();
        User user = systemService.getUser(principal.getId());
//		 model.addAttribute("categoryList", categoryService.findByUser(true, null));
        model.addAttribute("categoryList", categoryService.findByParentId("000000","1"));
        if(user.getUserType().equals("4")){
            model.addAttribute("userId",user.getId());
        }else{
            model.addAttribute("userId","");
        }
        return "modules/cms/cmsBusinessTree";
    }

	@RequiresPermissions("cms:view")
	@RequestMapping(value = "classifiedInfoTree")
	public String classifiedInfo(Model model) {
        SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
//        Office office = new Office();
        User user = systemService.getUser(principal.getId());
        String officeId = "";
//        while (!user.getOffice().getType().equals("5")){
//            officeId = officeService.get(user.getOffice().getParentId()).getId();
//        }
        if(user.getOffice().getType().equals("6")){
            officeId = officeService.get(user.getOffice().getParentId()).getId();
        }else if (user.getOffice().getType().equals("5")){
            officeId = user.getOffice().getId();
        }else{
            officeId = "";
        }
//		 model.addAttribute("categoryList", categoryService.findByUser(true, null));
		model.addAttribute("categoryList", categoryService.findByParentId("11","1"));
//        model.addAttribute("categoryList", categoryService.findByUser(true,null));
        model.addAttribute("officeId",officeId);
		return "modules/cms/classifiedInfoTree";
	}

	@RequiresPermissions("cms:view")
	@RequestMapping(value = "none")
	public String none() {
		return "modules/cms/cmsNone";
	}

}
